package com.teemo.sprinboot06mybatiscrud.controller;

import com.alibaba.fastjson.JSONObject;
import com.teemo.sprinboot06mybatiscrud.dto.DeliverLocalhsot;
import com.teemo.sprinboot06mybatiscrud.entity.DispatchFee;
import com.teemo.sprinboot06mybatiscrud.entity.TransactionRecord;
import com.teemo.sprinboot06mybatiscrud.entity.User;
import com.teemo.sprinboot06mybatiscrud.mapper.DispatchFeeMapper;
import com.teemo.sprinboot06mybatiscrud.mapper.TransactionRecordMapper;
import com.teemo.sprinboot06mybatiscrud.mapper.UserMapper;
import com.teemo.sprinboot06mybatiscrud.utils.MyJsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 骑手api
 */
@RestController
public class DeliverSideController {

    MyJsonResponse myJsonResponse = new MyJsonResponse();

    @Autowired
    UserMapper userMapper;

    @Autowired
    TransactionRecordMapper transactionRecordMapper;
    @Autowired
    DispatchFeeMapper dispatchFeeMapper;

    // 获取所有快递员列表
    @GetMapping("/getAllDeliver")
    public String getAllDeliver() {
        List<User> allDeliver = userMapper.getAllDeliver();
        ArrayList<String> strings = new ArrayList<>();
        for (User eachUser : allDeliver) {
            strings.add(eachUser.getUsername());
        }
        return myJsonResponse.make200Resp(MyJsonResponse.default_200_response, strings);
    }

    // 自动配单
    @PostMapping("/aotuDeliverGetOrder")
    public String aotuDeliverGetOrder(String orderNo) {

        // 1,查询可接单得骑手
        List<User> allDeliver = userMapper.getAllDeliverByGetOrder();
        if(CollectionUtils.isEmpty(allDeliver)) {
            return myJsonResponse.make500Resp("暂无空闲骑手", "false");
        }

        // 2,随机根据距离给骑手派订单
        List<TransactionRecord> recordList = transactionRecordMapper.getTransactionRecordByTransactionMD5(orderNo);


        ArrayList<DeliverLocalhsot> list = new ArrayList<DeliverLocalhsot>();
        allDeliver.forEach(deliver->{
            // 骑手 用户 得位置
            List<DispatchFee> juli1 = dispatchFeeMapper.getDispatchFeeByTwoCity(recordList.get(0).getUser_location(),deliver.getPlace());
            // 骑手 商家 得位置
            List<DispatchFee> juli2 = dispatchFeeMapper.getDispatchFeeByTwoCity(deliver.getPlace(),recordList.get(0).getOffer_location());
            if (CollectionUtils.isEmpty(juli1) || CollectionUtils.isEmpty(juli2)){
                return;
            }

            int i = Integer.parseInt(juli1.get(0).getLocalhost_distance()) + Integer.parseInt(juli2.get(0).getLocalhost_distance());
            DeliverLocalhsot deliverLocalhsot = new DeliverLocalhsot();
            deliverLocalhsot.setDeliverName(deliver.getUsername());
            deliverLocalhsot.setDistance(i);
            list.add(deliverLocalhsot);
        });

        Optional<DeliverLocalhsot> min = list.stream().min(Comparator.comparingInt(DeliverLocalhsot::getDistance));
        // 骑手已接单
        userMapper.updateDeliverStart("2",min.get().getDeliverName());

        return myJsonResponse.make200Resp(MyJsonResponse.default_200_response, min.get().getDeliverName());
    }




    // 获取该配送员的各订单数量（deliverHome 数据展示）
    @GetMapping("/getMyDeliverTaskNumber")
    public String getMyDeliverTaskNumber(HttpSession httpSession) {
        // 验证身份，只有快递员能访问接口
        Object username = httpSession.getAttribute("username");
        Object user_role = httpSession.getAttribute("user_role");
        if (!user_role.toString().equals("deliver")) {
            return myJsonResponse.make403Resp(MyJsonResponse.default_403_response);
        }
        List<TransactionRecord> myAllDeliverTask = transactionRecordMapper.getMyAllDeliverTask(username.toString());
        int dispatchingCount = 0;
        int finishedCount = 0;
        for (TransactionRecord eachTransaction : myAllDeliverTask) {
            switch (eachTransaction.getStatus()) {
                case 2: {
                    dispatchingCount++;
                    break;
                }
                case 3: {
                    finishedCount++;
                    break;
                }
            }
        }
        int totalCount = dispatchingCount + finishedCount;
        JSONObject retJson = new JSONObject();
        retJson.put("totalCount", totalCount);
        retJson.put("dispatchingCount", dispatchingCount);
        retJson.put("finishedCount", finishedCount);
        return myJsonResponse.make200Resp(MyJsonResponse.default_200_response, retJson);
    }

    // 获取所有配送的订单
    @GetMapping("/getMyAllDeliverTask")
    public String getMyAllDeliverTask(HttpSession httpSession) {
        // 验证身份，只有快递员能访问接口
        Object username = httpSession.getAttribute("username");
        Object user_role = httpSession.getAttribute("user_role");
        if (!user_role.toString().equals("deliver")) return myJsonResponse.make403Resp(MyJsonResponse.default_403_response);
        List<TransactionRecord> myAllDeliverTask = transactionRecordMapper.getMyAllDeliverTask(username.toString());
        return myJsonResponse.make200Resp(MyJsonResponse.default_200_response, myAllDeliverTask);
    }

    // 获取当前正配送的订单列表
    @GetMapping("/getMyDeliverTask")
    public String getMyDeliverTask(HttpSession httpSession) {
        // 验证身份，只有快递员能访问接口
        Object username = httpSession.getAttribute("username");
        Object user_role = httpSession.getAttribute("user_role");
        if (!user_role.toString().equals("deliver")) {
            return myJsonResponse.make403Resp(MyJsonResponse.default_403_response);
        }
        List<TransactionRecord> myDispatchingDeliverTask = transactionRecordMapper.getMyDispatchingDeliverTask(username.toString());
        return myJsonResponse.make200Resp(MyJsonResponse.default_200_response, myDispatchingDeliverTask);
    }

    // 获取已完成配送的订单列表
    @GetMapping("/getMyFinishedDeliverTask")
    public String getMyFinishedDeliverTask(HttpSession httpSession) {
        // 验证身份，只有快递员能访问接口
        Object username = httpSession.getAttribute("username");
        Object user_role = httpSession.getAttribute("user_role");
        if (!user_role.toString().equals("deliver")) {
            return myJsonResponse.make403Resp(MyJsonResponse.default_403_response);
        }
        List<TransactionRecord> myFinishedDeliverTask = transactionRecordMapper.getMyFinishedDeliverTask(username.toString());
        return myJsonResponse.make200Resp(MyJsonResponse.default_200_response, myFinishedDeliverTask);
    }

    // 将正配送的订单更改为完成状态
    @PostMapping("/setDeliverFinished")
    public String setDeliverFinished(@RequestParam("transaction_md5") String transaction_md5,
                                     HttpSession httpSession) {
        // 验证身份，只有快递员能访问接口
        Object username = httpSession.getAttribute("username");
        Object user_role = httpSession.getAttribute("user_role");
        if (!user_role.toString().equals("deliver")) {
            return myJsonResponse.make403Resp(MyJsonResponse.default_403_response);
        }
        // 验证transaction_md5 合法性
        List<TransactionRecord> transactionRecordByTransactionMD5 = transactionRecordMapper.getTransactionRecordByTransactionMD5(transaction_md5);
        if (transactionRecordByTransactionMD5.size() <= 0) return myJsonResponse.make500Resp(MyJsonResponse.default_500_response, "transaction_md5 error");
        // 更改为已完成配送状态
        Integer integer = transactionRecordMapper.setDeliverFinished(transaction_md5, username.toString());
        // 获取配送员之前的余额
        List<Double> userBalanceByUsername = userMapper.getUserBalanceByUsername(username.toString());
        if (userBalanceByUsername.size() <= 0) return myJsonResponse.make500Resp(MyJsonResponse.default_500_response, "user balance error");
        // 计算配送员新余额
        Double afterBalance = userBalanceByUsername.get(0) + transactionRecordByTransactionMD5.get(0).getDispatch_fee();
        // 更新配送员新的余额
        userMapper.updateUserBalance(username.toString(), afterBalance);
        // 更新配送员状态
        userMapper.updateUserStatus(username.toString());
        return myJsonResponse.make200Resp(MyJsonResponse.default_200_response, integer);
    }

}
